package com.droowa.util;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import com.droowa.domain.Product;

@Component
public class ProductView  extends AbstractExcelView{

	@SuppressWarnings("unchecked")
	@Override
	protected void buildExcelDocument(Map<String, Object> model,
			HSSFWorkbook workbook, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		response.setHeader("Content-Disposition", "attachment; filename=product.xls");
		HSSFSheet sheet = createFirstSheet(workbook);
		createColumnLabel(sheet);
		
		List<Product> product = (List<Product>)model.get("product");
		int rowNum=1;
		for(Product pro : product){
			createProductRow(sheet, pro, rowNum++);
		}
	}

	private HSSFSheet createFirstSheet(HSSFWorkbook workbook) {
		HSSFSheet sheet = workbook.createSheet();
		workbook.setSheetName(0, "상품정보");//0번 sheet에 업무기록이라는 이름 부여
		sheet.setColumnWidth((short)1, (short)(6000));
		sheet.setColumnWidth((short)2, (short)(5000));
		sheet.setColumnWidth((short)3, (short)(4000));
		sheet.setColumnWidth((short)4, (short)(2500));
		sheet.setColumnWidth((short)5, (short)(2500));
		sheet.setColumnWidth((short)6, (short)(3500));
		sheet.setColumnWidth((short)7, (short)(10000));
		return sheet;
	}
	
	private void createColumnLabel(HSSFSheet sheet) {
		HSSFRow firstRow = sheet.createRow(0);
		HSSFCell cell = firstRow.createCell((short) 0);
		cell.setCellValue("ID");
		
		cell = firstRow.createCell((short) 1);
		cell.setCellValue("이름");
		
		cell = firstRow.createCell((short) 2);
		cell.setCellValue("모델NO");		
		
		cell = firstRow.createCell((short) 3);
		cell.setCellValue("Brand");		
		
		cell = firstRow.createCell((short) 4);
		cell.setCellValue("재고");
		
		cell = firstRow.createCell((short) 5);
		cell.setCellValue("안전재고");

		cell = firstRow.createCell((short) 6);
		cell.setCellValue("카테고리명");
		
		cell = firstRow.createCell((short) 7);
		cell.setCellValue("상세설명");
		
	}
	
	private void createProductRow(HSSFSheet sheet, Product product, int rowNum) {
		HSSFRow row = sheet.createRow(rowNum);
		HSSFCell cell = row.createCell((short)0);
		cell.setCellValue(product.getId());
		
		cell = row.createCell((short)1);
		cell.setCellValue(product.getName());
		
		cell = row.createCell((short)2);
		cell.setCellValue(product.getModelNo());
		
		cell = row.createCell((short)3);
		cell.setCellValue(product.getBrand());
		
		cell = row.createCell((short)4);
		cell.setCellValue(product.getQuantity());
		
		cell = row.createCell((short)5);
		cell.setCellValue(product.getSafeQuantity());
		
		cell = row.createCell((short)6);
		cell.setCellValue(product.getCategoryName());
		
		cell = row.createCell((short)7);
		cell.setCellValue(product.getDescription());
		
	}
	
	
}
